<?php
/**
 * Created by PhpStorm.
 * User: caichunmin
 * 推荐接口
 * Date: 2017/1/9
 * Time: 17:37
 */

namespace YMApi\Controller;

use Think\Controller;

class RecommendController extends Controller
{
    protected $aliyun_url = 'https://shujuapi.aliyun.com/dataplus_129090/re/';
    protected function _initialize()
    {
        header("Access-Control-Allow-Origin: *");
    }
    //推荐结果列表
    public function lists(){
        $wid = I('wid',8, 'int');
        $user_id = I('uid',0, 'int');
        $goods_id = I('goods_id');
        $num = I('num',6, 'int');
        $page = I('page',1, 'int');
        if(!$wid || !$num){
            returnApiError('参数错误');
        }
        $page = $page>13?13:$page;
        $num = $num>150?150:$num;

        $return_data = array();
        $id_list = array();

        if($goods_id)
            $url = $this->aliyun_url ."doRec?biz_code=sdx_test&scn_code=details&user_id=$user_id&item_id=$goods_id";
        else
            $url = $this->aliyun_url ."doRec?biz_code=sdx_test&scn_code=home&user_id=$user_id";


        if($goods_id){
            $limit = $num;
            $data = get_recommend_shop($url);
        }else{
            $page_size = 12;
            $firstpage = ($page-1) * $page_size;
            $limit = $firstpage .','.$page_size;


            $key = md5('recommend'.$user_id);
            $data = S($key);
            if(!$data){
                $data = get_recommend_shop($url);
                //写入缓存（30分钟）
                S($key, $data, 60*30);
            }
        }

       // print_r($data);exit;


        //仓库过滤
        $id_list = $this->get_goods_id($data, $wid, $num, $goods_id);



        $map['goods_id'] = array('in', implode(',', $id_list));
        $map['is_on_sale'] = array('eq' , 1);
        $map['practical_num'] = array('neq',0);
        $map['is_delete'] = array('eq', 0);
        $map['is_offline'] = array('eq',0);
        $map['goods_img'] = array('neq', '');
        $map['cat_id'] = array('neq', 151); //过滤其他他分类（测试商品）

        if(!$goods_id){
            if(I('num')){
                //不分页
                $limit = $num;
            }else{
                //分页模式
                $goods_conut =  M('goods_wid_'.$wid)->where($map)->count();
                $goods_conut = $goods_conut > 150 ? 150 : $goods_conut;
                $pagecount = ceil($goods_conut/$page_size);

                if($pagecount <= $page){
                    $page_size_rui = floor (($pagecount*$page_size-$goods_conut)/3)*3;
                    if($page_size_rui==0){
                        //处理最后一页 恰好显示完全部数据时，导致分页丢失，全部显示的BUG   @poptser
                        $page_size_rui = 12;
                    }
                    $limit = $firstpage .','.$page_size_rui; //最后一页区3的整数条数
                    //判断是否为最后一页
                    $return_data['is_bottom'] = 1;
                }else{
                    $return_data['is_bottom'] = 0;
                }
                $return_data['total'] = $goods_conut;
                $return_data['page'] = array(
                    'firstpage' => $firstpage,
                    'pagecount' => $pagecount,
                    'pagesize' =>$page_size
                );
            }
        }
        $goods_list = D('Goods')->get_recomment_goods($wid, $user_id, $map, $limit);

        $return_data['goods_list'] = $goods_list ;
//        $return_data['sql'] = M('goods_wid_'.$wid)->getLastSql();
        //print_r($return_data);exit;
        if($goods_list)
            returnApiSuccess($return_data);
        else
            returnApiError('无数据');
    }

    protected function get_goods_id($data, $wid, $num, $goods_id){
        $id_list = array();
        foreach ($data['rec'] as $key=>$value){
            if($value['2'] != $goods_id){
                $id_list[] = $value['2'];
            }
        }
        return $id_list;
    }

    //启动预数据预处理
    public function data_dispose(){
        $url = $this->aliyun_url ."etl";
        $content = array(
            'biz_code'=>'sdx_test',
            'ds'=> date('Ymd', strtotime("-1 day"))
        );
        //获取推荐引擎返回数据
        $data = get_recommend_shop($url, $content);
        print_r($data);exit;

    }

    //查询任务运行结果
    public function task_status(){
        $task_id = I('get.task_id');
        $url = $this->aliyun_url ."status?task_id=$task_id";
        $data = get_recommend_shop($url);
        print_r($data);exit;
    }

    //启动离线任务
    public function start_task(){
        $url = $this->aliyun_url ."tasks";
        $content = array(
            'biz_code'=>'sdx_test',
            'ds'=> date('Ymd', strtotime("-1 day"))
        );
        //获取推荐引擎返回数据
        $data = get_recommend_shop($url, $content);
        print_r($data);exit;
    }
}